//
//  BBTabBarViewController.m
//  QuLuoMianApp
//
//  Created by zyz on 2017/12/19.
//  Copyright © 2017年 科大讯飞. All rights reserved.
//

#import "BBTabBarViewController.h"
#import "BBTabBar.h"
#import "ZWNavgationController.h"

#import "BBHomeViewController.h"
#import "BBEssensionViewController.h"
#import "BBTravelViewController.h"
#import "BBDiscoverViewController.h"
#import "BBMineViewController.h"

@interface BBTabBarViewController ()

@end

@implementation BBTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置item属性
    [self setupItem];
    
    // 添加所有的子控制器
    [self setupChildVcs];
    
    // 处理TabBar
    [self setupTabBar];
}

/**
 * 处理TabBar
 */
- (void)setupTabBar
{
    [self setValue:[[BBTabBar alloc] init] forKeyPath:@"tabBar"];
}

/**
 * 添加所有的子控制器
 */
- (void)setupChildVcs
{
    
    [self setupChildVc:[[BBHomeViewController alloc] init] title:@"首页" image:@"home_tabbar_home_icon" selectedImage:@"home_tabbar_home_icon"];
    
    [self setupChildVc:[[BBEssensionViewController alloc] init] title:@"精选" image:@"home_tabbar_essension_icon" selectedImage:@"home_tabbar_essension_icon"];
    
    [self setupChildVc:[[BBDiscoverViewController alloc] init] title:@"发现" image:@"home_tabbar_discover_icon" selectedImage:@"home_tabbar_discover_icon"];
    
    [self setupChildVc:[[BBTravelViewController alloc] init] title:@"旅图" image:@"home_tabbar_travel_icon" selectedImage:@"home_tabbar_travel_icon"];
    
     [self setupChildVc:[[BBMineViewController alloc] init] title:@"我的" image:@"home_tabbar_mine_icon" selectedImage:@"home_tabbar_mine_icon"];
}



/**
 * 添加一个子控制器
 * @param title 文字
 * @param image 图片
 * @param selectedImage 选中时的图片
 */
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 包装一个导航控制器
    ZWNavgationController *nav = [[ZWNavgationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    
    // 设置子控制器的tabBarItem
//    vc.title = title;
    nav.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}


/**
 * 设置item属性
 */
- (void)setupItem
{
    // UIControlStateNormal状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    // 文字颜色
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    // 文字大小
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    
    // UIControlStateSelected状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    // 文字颜色
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    
    // 统一给所有的UITabBarItem设置文字属性
    // 只有后面带有UI_APPEARANCE_SELECTOR的属性或方法, 才可以通过appearance对象来统一设置
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
